﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;

//use 
//"C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorerTool\ISETool" ts xd {80a0bc96-385c-4312-a5a3-5b8a6ffd7ad1} C:\Temp\IsoDump

namespace wordroid.Model
{
    public class WordroidDataContext : DataContext {
        public WordroidDataContext() : base("Data Source=isostore:/wordroid.sdf") {
            
        }       

        public Table<Word> words {
            get { return GetTable<Word>(); }
        }

        public Table<Translation> translations {
            get { return GetTable<Translation>(); }
        }
    }

    [Table]
    public class Word : INotifyPropertyChanged, INotifyPropertyChanging {

        [Column(IsPrimaryKey = true, IsDbGenerated = true, 
            DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int ID {
            get { return id; }
            set {
                if (id != value) {
                    id = value;
                }
            }
        }

        [Column(CanBeNull = false, DbType = "NVARCHAR(255)", AutoSync = AutoSync.Always)]
        public String originalWord {
            get { return _originalWord; }
            set {
                NotifyPropertyChanging("originalWord");
                _originalWord = value;
                NotifyPropertyChanged("originalWord");
            }
        }

        [Association(Name = "FK_translations", Storage = "_translations", ThisKey = "ID", OtherKey = "word_id")]
        public EntitySet<Translation> translations {
            get { return this._translations; }            
        }

        private void NotifyPropertyChanged(string propertyName) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void NotifyPropertyChanging(string propertyName){
            if (PropertyChanging != null){
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        private int id;
        private String _originalWord;
        private EntitySet<Translation> _translations = new EntitySet<Translation>();        

        public event PropertyChangedEventHandler PropertyChanged;
        public event PropertyChangingEventHandler PropertyChanging;
    }

    [Table]
    public class Translation : INotifyPropertyChanged, INotifyPropertyChanging {

        public Translation() {
            wordRef = new EntityRef<Word>();
            _isValid = false;
        }

        [Association(Name = "FK_translations", Storage = "wordRef", ThisKey = "word_id",
            OtherKey = "ID", IsForeignKey = true)]
        public Word Word {
            get { return wordRef.Entity; }
            set { 
                Word prev = wordRef.Entity;
                if(prev != value || wordRef.HasLoadedOrAssignedValue == false) {
                    if(prev != null) {
                        wordRef.Entity = null;
                        prev.translations.Remove(this);
                    }

                    this.wordRef.Entity = value;
                    if(value != null) {
                        value.translations.Add(this);
                        this.word_id = value.ID;
                    } else {
                        this.word_id = default(Nullable<int>);
                    }
                }
            }
        }

        [Column(IsPrimaryKey = true, IsDbGenerated = true,
            DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int PK
        {
            get { return _PK; }
            set {
                if (_PK != value) {
                    _PK = value;
                }
            }
        }

        [Column(CanBeNull = false, DbType = "NVARCHAR(255)", AutoSync = AutoSync.Always)]
        public String translation {
            get { return _translation; }
            set {
                NotifyPropertyChanging("translation");
                _translation = value;
                NotifyPropertyChanged("translation");
            }
        }

        [Column(CanBeNull = false)]
        public Boolean isValid
        {
            get { return this._isValid; }
            set
            {
                NotifyPropertyChanging("isValid");
                _isValid = value;
                NotifyPropertyChanged("isValid");
            }
        }

        [Column(CanBeNull = false, DbType = "INT")]
        public int? word_id
        {
            get { return this._word_id; }
            set
            {
                NotifyPropertyChanging("word_id");
                _word_id = value;
                NotifyPropertyChanged("word_id");
            }
        }

        private void NotifyPropertyChanged(string propertyName){
            if (PropertyChanged != null){
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void NotifyPropertyChanging(string propertyName){
            if (PropertyChanging != null){
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        private int _PK;
        private String _translation;
        private Boolean _isValid;
        private int? _word_id;
        private EntityRef<Word> wordRef;
        
        public event PropertyChangedEventHandler PropertyChanged;
        public event PropertyChangingEventHandler PropertyChanging;
    }
}